package com.raja.servingStation.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.raja.servingStation.common.constant.Constants;
import com.raja.servingStation.common.constant.MysqlPageVo;
import com.raja.servingStation.dao.NoticeDao;
import com.raja.servingStation.entity.Notice;
import com.raja.servingStation.entity.NoticeAttach;
import com.raja.servingStation.entity.NoticeSubscribe;
import com.raja.servingStation.entity.vo.NoticeReadingDetail;
import com.raja.servingStation.entity.vo.NoticeState;
import com.raja.servingStation.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 公文服务实现
 * 2017/9/30
 */
@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    NoticeDao noticeDao;


//    /**
//     * 分布式事物控制
//     * @throws Exception
//     */
//    @Override
//    public void save() throws Exception {
//        testRepairDao.update();
//    }

//    @Override
//    public void queryNoticeList() throws Exception {
//        noticeDao.queryNoticeList();
//    }



    @Override
    public MysqlPageVo queryOrderListByPage(JSONObject body) throws Exception {
        Integer pageSize = body.getInteger("pageSize");
        Integer currPage = body.getInteger("currPage");
        MysqlPageVo mysqlPageVo = new MysqlPageVo();
        if (pageSize == null)
            pageSize = Constants.pageSize;
        mysqlPageVo.setPageSize(pageSize, currPage);
        int count = noticeDao.cntNoticeList(body).intValue();
        mysqlPageVo.setTotal(count);
        if (count > 0) {
            List<Notice> notices = noticeDao.queryNoticeListByPage(body, mysqlPageVo);

            for (Notice notice : notices) {
                notice.setNoticeAttachs(noticeDao.queryNoticeAttachs(notice.getId()));

                //未发送
                Long notSendingNums = noticeDao.getNotSendingNums(notice.getId());
                notice.setNotSendingNum(notSendingNums.intValue());

                List<NoticeState> noticeStates = noticeDao.getNoticeStates(notice.getId());
                for (NoticeState noticeState : noticeStates) {
                    switch (noticeState.getState()) {
                        case 2: //2、发送未读
                            // 统计发送总数 case2+case3
//                            if(null!=notice.getSendingNum()){
                                notice.setSendingNum(notice.getSendingNum()+noticeState.getStateNum());
//                            }else{
//                                notice.setSendingNum(noticeState.getStateNum());
//                            }
                            //2、发送未读
                            notice.setSendingAndNotReadingNum(noticeState.getStateNum());
                            break;
                        case 3: //3、发送已读
                            // 统计发送总数 case2+case3
//                            if (null != notice.getSendingNum()) {
                                notice.setSendingNum(notice.getSendingNum() + noticeState.getStateNum());
//                            } else {
//                                notice.setSendingNum(noticeState.getStateNum());
//                            }
                            break;
                        default:
                            System.out.println("未匹配阅读数量");
                            break;
                    }
                }
            }

            mysqlPageVo.setData(notices);
        }
        return mysqlPageVo;
    }


    @Override
    public void addNotice(JSONObject body) throws Exception {
        noticeDao.publishNotice(body);
    }

    @Override
    public List<NoticeReadingDetail> queryNoticeReadingDetail(JSONObject body) throws Exception {
        return noticeDao.queryNoticeReadingDetail(body.getLong("noticeId"));
    }

    @Override
    public void sendNotice2Station(JSONObject body) throws Exception {
        Long noticeId = body.getLong("noticeId");
        Long stationId = body.getLong("stationId");
        noticeDao.sendNotice2Station(noticeId, stationId);
    }


    @Override
    public void cancelNotice(JSONObject body) throws Exception {
        Long noticeId = body.getLong("id");
        noticeDao.cancelNotice(noticeId);
    }


}
